package com.tools.ad.adscene;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Keep;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.google.android.gms.ads.AdListener;
import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.AdsContants;
import com.hm.adsbuisiness.bean.HmAdPlace;
import com.hm.adsbuisiness.bean.HmAdUnionPlace;
import com.hm.adsbuisiness.bean.RawAd;
import com.hm.adsbuisiness.bean.banner.AMNRawBannerAd;
import com.hm.adsbuisiness.bean.banner.AMRawBannerAd;
import com.hm.adsbuisiness.bean.banner.MpRawBannerAd;
import com.hm.adsbuisiness.bean.banner.RawBannerAd;
import com.hm.adsbuisiness.bean.interstitial.AMNRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.AMRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.FluctRawIntersitialAd;
import com.hm.adsbuisiness.bean.interstitial.MpRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.RawInterstitialAd;
import com.hm.adsbuisiness.bean.listener.BatmobiAdLoadListener;
import com.hm.adsbuisiness.bean.listener.banner.BannerLoadListener;
import com.hm.adsbuisiness.bean.listener.interstitialad.InterstitialAdLoadListener;
import com.hm.adsbuisiness.bean.listener.nativead.NativeAdLoadListener;
import com.hm.adsbuisiness.bean.listener.reward.RewardAdListener;
import com.hm.adsbuisiness.bean.nativead.AmRawNativeAd;
import com.hm.adsbuisiness.bean.nativead.AmUniRawNativeAd;
import com.hm.adsbuisiness.bean.nativead.BatRawNativeAd;
import com.hm.adsbuisiness.bean.nativead.DuRawNativeAd;
import com.hm.adsbuisiness.bean.nativead.MpRawNativeAd;
import com.hm.adsbuisiness.bean.nativead.RawNativeAd;
import com.hm.adsbuisiness.bean.reward.AMNRawRewardAd;
import com.hm.adsbuisiness.bean.reward.FluctRawRewardAd;
import com.hm.adsbuisiness.bean.reward.RawRewardAd;
import com.hm.adsbuisiness.service.AdActionException;
import com.hm.adsbuisiness.utils.AdUtils;
import com.hm.adsui.card.AdCardEventListener;
import com.hm.adsui.card.CommonNativeAdCardView;
import com.hm.adsui.card.SmallNativeAdCardView;
import com.hm.base.android.mob.AMApplication;
import com.tools.ad.ADModule;
import com.tools.ad.AdType;
import com.tools.ad.EntranceType;
import com.tools.ad.R;
import com.tools.ad.util.AppUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * desc: 按广告配置顺序加载广告，默认DAP垫底
 * date: 2018/2/1
 * author: ancun
 */
public abstract class AdStrategyScene extends AdScene {

    /**
     * 当前加载好的广告
     */
    protected RawAd activeAd;
    /**
     * 加载好的Du广告
     */
    protected RawNativeAd loadedDapAd;
    protected boolean supportBannerRefresh = false;
    private StrategyAdCommon mStrategyAdCommon;

    @Keep
    public AdStrategyScene(AMApplication context, EntranceType entranceType) {
        super(context, entranceType);
    }

    protected ADModule getADModule() {
        return (ADModule) imContext.getSubModule(ADModule.MODULE_KEY);
    }

    public RawAd getActiveAd() {
        return activeAd;
    }

    /**
     * 请求DAP AM两个平台的广告
     * at 2018/2/1 16:41
     * by ancun
     */
    protected void onRequestAll() {

    }

    /**
     * 请求DAP AM两个平台的广告失败
     * at 2018/2/1 16:41
     * by ancun
     */
    protected void onRequestAllFail() {

    }

    /**
     * DAP加载native广告成功，返回的是DAP广告
     */
    protected void onADLoadedWithDAP() {

    }

    /**
     * BANNER广告刷新回调
     *
     * @param type
     */
    protected void onADRefresh(AdType type) {

    }

    protected void onADFilledAndLoaded(AdType adType) {
        onADFilled(adType);
        onADLoaded(adType);
    }

    /**
     * 广告需要 Activity
     *
     * @param adType
     */
    protected void onADLoadByActivity(AdType adType) {

    }

    /**
     * 初始化广告viwe对象
     *
     * @return
     */
    protected abstract CommonNativeAdCardView initCardView();

    /**
     * 请求广告成功，并可以加载广告视图
     * 包含广告类型 DA_NATIVE 、 FB_NATIVE 、 AM_BANNER
     * at 2018/2/1 16:42
     * by ancun
     *
     * @param adType
     */
    protected abstract void onADLoaded(AdType adType);

    /**
     * 点击广告，包含广告类型 DA_NATIVE 、 FB_NATIVE 、 AM_BANNER
     * at 2018/2/1 16:42
     * by ancun
     *
     * @param adType
     */
    protected abstract void onADClick(AdType adType);

    /**
     * 展示广告，包含广告类型 DA_NATIVE 、 FB_NATIVE 、 AM_BANNER
     * at 2018/2/1 16:42
     * by ancun
     *
     * @param adType
     */
    protected abstract void onADShow(AdType adType);

    protected abstract void onAdClose(AdType adType);

    /**
     * 请求广告,区分DAP 、ADMOB两个平台
     * at 2018/2/1 16:41
     * by ancun
     *
     * @param adType
     */
    protected abstract void onADLoad(AdType adType);

    /**
     * 请求广告失败,包含错误码,区分DAP 、ADMOB两个平台
     * at 2018/2/1 16:41
     * by ancun
     */
    protected abstract void onADFail(AdType adType, int code);

    /**
     * 广告返回数据
     *
     * @param adType
     */
    protected abstract void onADFilled(AdType adType);

    @Override
    public void release() {
        activeAd = null;
        loadedDapAd = null;
        mStrategyAdCommon = null;
        destroyUnionPlaceAd();
    }

    protected void destroyUnionPlaceAd() {
        try {
            HmAdUnionPlace unionPlace = HmAdsSDK.getInstance().getUnionAd(mEntranceType.getPid());
            if (unionPlace != null) {
                unionPlace.destory();
            }
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据配置中 ads 中配置的个数及顺序，自动选择策略
     *
     * @param unionPlace
     * @return
     */
    protected boolean loadUnionPlace(HmAdUnionPlace unionPlace) {
        List<RawAd> rawAdList = unionPlace.getRawAdList();
        if (rawAdList == null) {
            return false;
        }

        if (mStrategyAdCommon == null) {
            mStrategyAdCommon = new StrategyAdCommon();
        }

        return mStrategyAdCommon.load(unionPlace);
    }

    protected boolean reloadUnionPlace(HmAdUnionPlace unionPlace) {
        List<RawAd> rawAdList = unionPlace.getRawAdList();
        if (rawAdList == null) {
            return false;
        }

        if (mStrategyAdCommon == null) {
            mStrategyAdCommon = new StrategyAdCommon();
        }

        return mStrategyAdCommon.reload(unionPlace);
    }

    @Override
    public View getAdView() {
        HmAdUnionPlace unionPlace = HmAdsSDK.getInstance().getUnionAd(mEntranceType.getPid());
        if (unionPlace == null || activeAd == null) {
            return null;
        }

        if (activeAd instanceof MpRawBannerAd) { //必须先判断mopub banner
            return getMpRawBannerAdView((MpRawBannerAd) activeAd);
        } else if (activeAd instanceof AMRawBannerAd) {
            return getRawBannerAdView((AMRawBannerAd) activeAd);
        } else if (activeAd instanceof MpRawNativeAd) {
            return getMpRawNativeAdView(unionPlace, (MpRawNativeAd) activeAd);
        } else if (activeAd instanceof RawNativeAd) {
            return getRawNativeAdView(unionPlace, (RawNativeAd) activeAd);
        } else {
            return null;
        }
    }

    public BatRawNativeAd getBatRawNativeAd() {
        if (activeAd != null && activeAd instanceof BatRawNativeAd) {
            return (BatRawNativeAd) activeAd;
        }
        return null;
    }

    private View getMpRawNativeAdView(HmAdPlace adPlace, MpRawNativeAd mpRawNativeAd) {
        SmallNativeAdCardView view = new SmallNativeAdCardView(imContext);
        try {
            onADShow(AdType.MP_NATIVE);
            view.bindRawAd(adPlace, mpRawNativeAd);
        } catch (AdActionException e) {
            e.printStackTrace();
        }
        return view;
    }

    private View getRawNativeAdView(HmAdPlace adPlace, RawNativeAd rawNativeAd) {
        try {
            CommonNativeAdCardView adCardView = null;
            adCardView = initCardView();
            if (adCardView == null) {
                return null;
            }
            adCardView.setEventListener(new AdCardEventListener() {
                @Override
                public void onAdCardShow(HmAdPlace hmNativeAdPlace) {
                    if (AdUtils.isFaceBook(activeAd)) {
                        onADShow(AdType.FB_NATIVE);
                    } else if (AdUtils.isAdmob(activeAd)) {
                        if (activeAd.getPlatform().equals("am")) {
                            onADShow(AdType.AM_NATIVE);
                        } else if (activeAd.getPlatform().equals("am_new")) {
                            onADShow(AdType.AM_NEW_NATIVE);
                        }
                    } else if (AdUtils.isMopub(activeAd)) {
                        onADShow(AdType.MP_NATIVE);
                    } else {
                        onADShow(AdType.DA_NATIVE);
                    }
                }
            });
            adCardView.bindRawAd(adPlace, rawNativeAd);
            return adCardView;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private View getMpRawBannerAdView(MpRawBannerAd mpRawBannerAd) {
        final View bannerView = mpRawBannerAd.getBannerView();
        if (bannerView == null) {
            return null;
        }
        if (bannerView.getParent() != null) {
            ViewGroup viewGroup = (ViewGroup) bannerView.getParent();
            viewGroup.removeAllViews();
        }
        bannerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                bannerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                onADShow(AdType.MP_BANNER);
            }
        });
        return bannerView;
    }

    private View getRawBannerAdView(AMRawBannerAd aMRawBannerAd) {
        final View bannerView = aMRawBannerAd.getBannerView();
        if (bannerView == null) {
            return null;
        }
        if (bannerView.getParent() != null) {
            ViewGroup viewGroup = (ViewGroup) bannerView.getParent();
            viewGroup.removeAllViews();
        }
        bannerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                bannerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                onADShow(AdType.AM_BANNER);
            }
        });
        return bannerView;
    }

    protected String getFormatEntranceNameByRawAd() {
        String adapterName = "";
        if (activeAd instanceof AMRawBannerAd) {
            try {
                adapterName = ((AMRawBannerAd) activeAd).getMediationAdapterClassName();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (activeAd instanceof AMRawInterstitialAd) {
            try {
                adapterName = ((AMRawInterstitialAd) activeAd).getMediationAdapterClassName();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (activeAd instanceof AmRawNativeAd) {
            try {
                adapterName = ((AmRawNativeAd) activeAd).getMediationAdapterClassName();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (!TextUtils.isEmpty(adapterName) && !adapterName.toLowerCase().contains("admob") && !adapterName.toLowerCase().equals("error")) {
            adapterName = "_" + adapterName;
        } else {
            adapterName = "";
        }

        return mEntranceType.getName() + adapterName;
    }

    protected int getVideoLayoutId() {
        return R.layout.small_mp_video_ad_card_layout;
    }

    protected int getLayoutId() {
        return R.layout.small_mp_card_layout;
    }

    public class StrategyAdCommon {
        private int currentAdIndex;
        private List<RawAd> execAdList;

        public StrategyAdCommon() {
            currentAdIndex = 0;
            execAdList = new ArrayList<>();
        }

        public boolean load(HmAdUnionPlace unionPlace) {
            List<RawAd> adList = unionPlace.getRawAdList();
            execAdList.clear();
            //如果未安装facebook将dap广告类型移到列表最后
            if (!isFaceBookInstall()) {
                for (RawAd ad : adList) {
                    if (!ad.getPlatform().equals(AdsContants.AD_PLATFORM_DAP)) {
                        execAdList.add(ad);
                    }
                }
                for (RawAd ad : adList) {
                    if (ad.getPlatform().equals(AdsContants.AD_PLATFORM_DAP)) {
                        execAdList.add(ad);
                    }
                }
            } else {
                execAdList.addAll(adList);
            }
            currentAdIndex = 0;
            onRequestAll();
            return loadRawAd();
        }

        public boolean reload(HmAdUnionPlace unionPlace) {
            return loadRawAd();
        }

        public boolean loadRawAd() {
            if (currentAdIndex >= execAdList.size()) {
                //广告已经请求完，判断是否有请求好的dap广告，如果有则使用该广告
                if (loadedDapAd != null) {
                    activeAd = loadedDapAd;
                    onADFilledAndLoaded(AdType.DA_NATIVE);
                    return true;
                }
                return false;
            }

            RawAd ad = execAdList.get(currentAdIndex);
            if (ad instanceof DuRawNativeAd) {
                return loadDuNativeAd((RawNativeAd) ad);
            } else if (ad instanceof AmRawNativeAd) {
                return loadAdmobNativeAd((AmRawNativeAd) ad);
            } else if (ad instanceof AMRawBannerAd) {
                return loadAmBannerAd((AMRawBannerAd) ad);
            } else if (ad instanceof AMRawInterstitialAd) {
                return loadAmInterstitialAd((AMRawInterstitialAd) ad);
            } else if (ad instanceof BatRawNativeAd) {
                return loadBatRawNativeAd((BatRawNativeAd) ad);
            } else if (ad instanceof MpRawNativeAd) {
                return loadMpRawNativeAd((MpRawNativeAd) ad);
            } else if (ad instanceof MpRawBannerAd) {
                return loadMpRawBannerAd((MpRawBannerAd) ad);
            } else if (ad instanceof MpRawInterstitialAd) {
                return loadMpRawInterstitialAd((MpRawInterstitialAd) ad);
            } else if (ad instanceof FluctRawIntersitialAd) {
                return loadFtInterstitialAd((FluctRawIntersitialAd) ad);
            } else if (ad instanceof FluctRawRewardAd) {
                return loadFtRawRewardAd((FluctRawRewardAd) ad);
            } else if (ad instanceof AmUniRawNativeAd) {
                return loadAmNRawNativeAd((AmUniRawNativeAd) ad);
            } else if (ad instanceof AMNRawBannerAd) {
                return loadAmNRawBannerAd((AMNRawBannerAd) ad);
            } else if (ad instanceof AMNRawInterstitialAd) {
                return loadAmNRawInterstitialAd((AMNRawInterstitialAd) ad);
            } else if (ad instanceof AMNRawRewardAd) {
                return loadAmNRawRewardAd((AMNRawRewardAd) ad);
            } else {
                return false;
            }
        }

        //加载admob原生广告
        private boolean loadAdmobNativeAd(final AmRawNativeAd ad) {
            if (ad == null) {
                return false;
            }
            try {
                ad.setAdmobAdListener(new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int i) {
                        super.onAdFailedToLoad(i);
                        onADFail(AdType.AM_NATIVE, i);
                        currentAdIndex++;
                        if (!loadRawAd()) {
                            onRequestAllFail();
                        }
                    }

                    @Override
                    public void onAdLeftApplication() {
                        super.onAdLeftApplication();
                        //应用外admob高级原生广告所有点击次数
                        onADClick(AdType.AM_NATIVE);
                    }
                });
                ad.setAdListener(new NativeAdLoadListener() {
                    @Override
                    public void onError(RawNativeAd rawNativeAd, int i, String s) {
                        //admob高级原生不会执行这个方法
                    }

                    @Override
                    public void onAdLoaded(RawNativeAd rawNativeAd) {
                        activeAd = rawNativeAd;
                        onADFilledAndLoaded(AdType.AM_NATIVE);
                    }

                    @Override
                    public void onClick(RawNativeAd rawNativeAd) {
                        //admob高级原生不会执行这个方法
                    }

                    @Override
                    public void onInstall(RawNativeAd rawNativeAd, String s) {
                        //admob高级原生不会执行这个方法
                    }

                    @Override
                    public void onLoggingImpression(RawNativeAd rawNativeAd) {
                        //admob高级原生不会执行这个方法
                    }
                });
                ad.load();
                onADLoad(AdType.AM_NATIVE);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        private boolean loadAmBannerAd(final RawBannerAd bannerAd) {
            if (bannerAd == null) {
                return false;
            }
            bannerAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    onAdClose(AdType.AM_BANNER);
                }

                @Override
                public void onAdFailedToLoad(int i) {
                    super.onAdFailedToLoad(i);
                    onADFail(AdType.AM_BANNER, i);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onAdLeftApplication() {
                    super.onAdLeftApplication();
                    onADClick(AdType.AM_BANNER);
                }

                @Override
                public void onAdOpened() {
                    super.onAdOpened();
                }

                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    if (supportBannerRefresh && activeAd != null && activeAd instanceof RawBannerAd) {
                        onADRefresh(AdType.AM_BANNER);
                    } else {
                        activeAd = bannerAd;
                        onADFilledAndLoaded(AdType.AM_BANNER);
                    }
                }
            });
            try {
                bannerAd.load();
                onADLoad(AdType.AM_BANNER);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        public boolean loadAmInterstitialAd(final AMRawInterstitialAd interstitialAd) {
            Context windowsToken = AMApplication.getInstance().getMWindowToken();
            if (windowsToken == null || !(windowsToken instanceof Activity)) {
                onADLoadByActivity(AdType.AM_INTERSTITIAL);
                return false;
            }
            if (interstitialAd == null) {
                return false;
            }
            try {
                if (interstitialAd.isLoaded() && !AppUtils.isInSelfApp()) {
                    activeAd = interstitialAd;
                    onADFilledAndLoaded(AdType.AM_INTERSTITIAL);
                    return true;
                }
            } catch (AdActionException e) {
                e.printStackTrace();
            }
            interstitialAd.setAmAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    super.onAdClosed();
                    onAdClose(AdType.AM_INTERSTITIAL);
                }

                @Override
                public void onAdFailedToLoad(int i) {
                    super.onAdFailedToLoad(i);
                    onADFail(AdType.AM_INTERSTITIAL, i);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onAdLeftApplication() {
                    super.onAdLeftApplication();
                    onADClick(AdType.AM_INTERSTITIAL);
                }

                @Override
                public void onAdOpened() {
                    onADShow(AdType.AM_INTERSTITIAL);
                }

                @Override
                public void onAdLoaded() {
                    activeAd = interstitialAd;
                    onADFilledAndLoaded(AdType.AM_INTERSTITIAL);
                }
            });

            try {
                interstitialAd.load();
                onADLoad(AdType.AM_INTERSTITIAL);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }

            return true;
        }

        private boolean loadDuNativeAd(RawNativeAd nativeAd) {
            if (nativeAd == null) {
                return false;
            }
            try {
                nativeAd.setAdListener(new NativeAdLoadListener() {
                    @Override
                    public void onError(RawNativeAd rawNativeAd, int i, String s) {
                        onADFail(AdType.DA_NATIVE, i);
                        currentAdIndex++;
                        if (!loadRawAd()) {
                            onRequestAllFail();
                        }
                    }

                    @Override
                    public void onAdLoaded(RawNativeAd rawNativeAd) {
                        activeAd = rawNativeAd;
                        if (AdUtils.isFaceBook(activeAd)) {
                            onADFilledAndLoaded(AdType.FB_NATIVE);
                        } else if (AdUtils.isAdmob(activeAd)) {
                            onADFilledAndLoaded(AdType.AM_NATIVE);
                        } else {
                            //dap优先级最后，把dap保存起来，先请求后面的广告
                            onADLoadedWithDAP();
                            loadedDapAd = rawNativeAd;
                            currentAdIndex++;
                            if (!loadRawAd()) {
                                onRequestAllFail();
                            }
                        }
                    }

                    @Override
                    public void onClick(RawNativeAd rawNativeAd) {
                        if (AdUtils.isFaceBook(activeAd)) {
                            onADClick(AdType.FB_NATIVE);
                        } else if (AdUtils.isAdmob(activeAd)) {
                            onADClick(AdType.AM_NATIVE);
                        } else {
                            onADClick(AdType.DA_NATIVE);
                        }
                    }

                    @Override
                    public void onInstall(RawNativeAd rawNativeAd, String s) {

                    }

                    @Override
                    public void onLoggingImpression(RawNativeAd rawNativeAd) {

                    }

                });
                nativeAd.load();
                onADLoad(AdType.DA_NATIVE);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        //加载batmobi原生广告
        private boolean loadBatRawNativeAd(final BatRawNativeAd ad) {
            if (ad == null) {
                return false;
            }
            try {
                ad.setBatAdListener(new BatmobiAdLoadListener() {
                    @Override
                    public void onAdLoadFinish(Object o) {
                        activeAd = ad;
                        onADFilledAndLoaded(AdType.BAT_NATIVE);
                    }

                    @Override
                    public void onAdError(Object adError) {
                        onADFail(AdType.BAT_NATIVE, -1);
                    }

                    @Override
                    public void onAdShowed() {
                        onADShow(AdType.BAT_NATIVE);
                    }

                    @Override
                    public void onAdClosed() {
                        onAdClose(AdType.BAT_NATIVE);
                    }

                    @Override
                    public void onAdClicked() {
                        onADClick(AdType.BAT_NATIVE);
                    }

                });
                ad.load();
                onADFilledAndLoaded(AdType.BAT_NATIVE);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        //加载mopub的原生广告
        public boolean loadMpRawNativeAd(final MpRawNativeAd ad) {
            if (ad == null) {
                return false;
            }
            ad.setAdListener(new NativeAdLoadListener() {
                @Override
                public void onError(RawNativeAd ad, int code, String msg) {
                    onADFail(AdType.MP_NATIVE, code);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onAdLoaded(RawNativeAd ad) {
                    activeAd = ad;
                    onADFilledAndLoaded(AdType.MP_NATIVE);
                }

                @Override
                public void onClick(RawNativeAd ad) {
                    onADClick(AdType.MP_NATIVE);
                }

                @Override
                public void onInstall(RawNativeAd ad, String packageName) {
                }

                @Override
                public void onLoggingImpression(RawNativeAd ad) {
                }

            });
            try {
                ad.setLayoutId(getLayoutId());
                ad.setVideoLayoutId(getVideoLayoutId());
                ad.load();
                onADLoad(AdType.MP_NATIVE);
            } catch (AdActionException e) {
                e.printStackTrace();
            }
            return true;
        }

        //加载mopub的banner广告
        private boolean loadMpRawBannerAd(final MpRawBannerAd ad) {
            if (ad == null) {
                return false;
            }
            ad.setAdListener(new BannerLoadListener() {

                @Override
                public void onError(RawBannerAd ad, int i, String msg) {
                    onADFail(AdType.MP_BANNER, i);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onAdLoaded(RawBannerAd ad) {
                    activeAd = ad;
                    onADFilledAndLoaded(AdType.MP_BANNER);
                }

                @Override
                public void onClick(RawBannerAd ad) {
                    onADClick(AdType.MP_BANNER);
                }

                @Override
                public void onInstall(RawBannerAd ad, String packageName) {

                }

                @Override
                public void onLoggingImpression(RawBannerAd ad) {

                }
            });
            try {
                ad.load();
                onADLoad(AdType.MP_BANNER);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        //加载mopub的插屏广告
        public boolean loadMpRawInterstitialAd(final MpRawInterstitialAd ad) {
            if (ad == null) {
                return false;
            }
            ad.setAdListener(new InterstitialAdLoadListener() {
                @Override
                public void onError(RawInterstitialAd ad, int code, String msg) {
                    onADFail(AdType.MP_INTERSTITIAL, code);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onAdLoaded(RawInterstitialAd ad) {
                    activeAd = ad;
                    onADFilledAndLoaded(AdType.MP_INTERSTITIAL);
                }

                @Override
                public void onShow(RawInterstitialAd ad) {
                    onADShow(AdType.MP_INTERSTITIAL);
                }

                @Override
                public void onClick(RawInterstitialAd ad) {
                    onADClick(AdType.MP_INTERSTITIAL);
                }

                @Override
                public void onClose(RawInterstitialAd ad) {
                    onAdClose(AdType.MP_INTERSTITIAL);
                }
            });
            try {
                ad.load();
                onADLoad(AdType.MP_INTERSTITIAL);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        public boolean loadFtInterstitialAd(final FluctRawIntersitialAd ad) {
            if (ad == null) {
                return false;
            }

            ad.setAdListener(new InterstitialAdLoadListener() {

                @Override
                public void onAdLoaded(RawInterstitialAd ad) {
                    activeAd = ad;
                    onADFilledAndLoaded(AdType.FT_INTERSTITIAL);
                }

                @Override
                public void onError(RawInterstitialAd ad, int code, String msg) {
                    onADFail(AdType.FT_INTERSTITIAL, code);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onShow(RawInterstitialAd ad) {
                    onADShow(AdType.FT_INTERSTITIAL);
                }

                @Override
                public void onClick(RawInterstitialAd ad) {
                    onADClick(AdType.FT_INTERSTITIAL);
                }

                @Override
                public void onClose(RawInterstitialAd ad) {
                    onAdClose(AdType.FT_INTERSTITIAL);
                }
            });

            try {
                onADLoad(AdType.FT_INTERSTITIAL);
                ad.load();
            } catch (AdActionException e) {
                e.printStackTrace();
            }

            return true;
        }

        private boolean loadFtRawRewardAd(FluctRawRewardAd ad) {
            Context windowsToken = AMApplication.getInstance().getMWindowToken();
            if (windowsToken == null || !(windowsToken instanceof Activity)) {
                onADLoadByActivity(AdType.FT_REWARD);
                return false;
            }

            ad.setAdListener(new RewardAdListener() {
                @Override
                public void onRewardedAdFailedToLoad(RawRewardAd ad, int code, String msg) {
                    currentAdIndex++;
                    onADFail(AdType.FT_REWARD, code);
                }

                @Override
                public void onRewardedAdLoaded(RawRewardAd ad) {
                    activeAd = ad;
                    onADFilledAndLoaded(AdType.FT_REWARD);
                }

                @Override
                public void onRewardedAdOpened(RawRewardAd ad) {
                    onADShow(AdType.FT_REWARD);
                }

                @Override
                public void onRewardedVideoStarted(RawRewardAd ad) {
                }

                @Override
                public void onRewardedAdLeftApplication(RawRewardAd ad) {
                }

                @Override
                public void onRewardedAdClosed(RawRewardAd ad) {
                    onAdClose(AdType.FT_REWARD);
                }

                @Override
                public void onRewarded(RawRewardAd ad) {
                }
            });

            try {
                onADLoad(AdType.FT_REWARD);
                ad.load((Activity) windowsToken);
            } catch (AdActionException e) {
                e.printStackTrace();
            }

            return true;
        }

        /**
         * 加载AdMob原生广告（新）
         *
         * @param ad
         * @return
         */
        private boolean loadAmNRawNativeAd(AmUniRawNativeAd ad) {
            if (ad == null) {
                return false;
            }

            try {
                ad.setAdmobAdListener(new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int i) {
                        super.onAdFailedToLoad(i);
                        onADFail(AdType.AM_NEW_NATIVE, i);
                        currentAdIndex++;
                        if (!loadRawAd()) {
                            onRequestAllFail();
                        }
                    }

                    @Override
                    public void onAdLeftApplication() {
                        super.onAdLeftApplication();
                        //应用外admob高级原生广告所有点击次数
                        onADClick(AdType.AM_NEW_NATIVE);
                    }
                });
                ad.setAdListener(new NativeAdLoadListener() {
                    @Override
                    public void onError(RawNativeAd rawNativeAd, int i, String s) {
                        //admob高级原生不会执行这个方法
                    }

                    @Override
                    public void onAdLoaded(RawNativeAd rawNativeAd) {
                        activeAd = rawNativeAd;
                        onADFilledAndLoaded(AdType.AM_NEW_NATIVE);
                    }

                    @Override
                    public void onClick(RawNativeAd rawNativeAd) {
                        //admob高级原生不会执行这个方法
                    }

                    @Override
                    public void onInstall(RawNativeAd rawNativeAd, String s) {
                        //admob高级原生不会执行这个方法
                    }

                    @Override
                    public void onLoggingImpression(RawNativeAd rawNativeAd) {
                        //admob高级原生不会执行这个方法
                    }
                });
                ad.load();
                onADLoad(AdType.AM_NEW_NATIVE);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }

            return true;
        }

        /**
         * 加载 AdMob Banner 广告（新）
         *
         * @param bannerAd
         * @return
         */
        private boolean loadAmNRawBannerAd(final AMNRawBannerAd bannerAd) {
            if (bannerAd == null) {
                return false;
            }
            bannerAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    onAdClose(AdType.AM_NEW_BANNER);
                }

                @Override
                public void onAdFailedToLoad(int i) {
                    super.onAdFailedToLoad(i);
                    onADFail(AdType.AM_NEW_BANNER, i);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onAdLeftApplication() {
                    super.onAdLeftApplication();
                    onADClick(AdType.AM_NEW_BANNER);
                }

                @Override
                public void onAdOpened() {
                    super.onAdOpened();
                }

                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    if (supportBannerRefresh && activeAd != null && activeAd instanceof RawBannerAd) {
                        onADRefresh(AdType.AM_NEW_BANNER);
                    } else {
                        activeAd = bannerAd;
                        onADFilledAndLoaded(AdType.AM_NEW_BANNER);
                    }
                }
            });
            try {
                bannerAd.load();
                onADLoad(AdType.AM_NEW_BANNER);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        /**
         * 加载AdMob插屏广告（新）
         *
         * @param interstitialAd
         * @return
         */
        private boolean loadAmNRawInterstitialAd(final AMNRawInterstitialAd interstitialAd) {
            Context windowsToken = AMApplication.getInstance().getMWindowToken();
            if (windowsToken == null || !(windowsToken instanceof Activity)) {
                onADLoadByActivity(AdType.AM_NEW_INTERSTITIAL);
                return false;
            }
            if (interstitialAd == null) {
                return false;
            }
            try {
                if (interstitialAd.isLoaded() && !AppUtils.isInSelfApp()) {
                    activeAd = interstitialAd;
                    onADFilledAndLoaded(AdType.AM_NEW_INTERSTITIAL);
                    return true;
                }
            } catch (AdActionException e) {
                e.printStackTrace();
            }
            interstitialAd.setAmAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    super.onAdClosed();
                    onAdClose(AdType.AM_NEW_INTERSTITIAL);
                }

                @Override
                public void onAdFailedToLoad(int i) {
                    super.onAdFailedToLoad(i);
                    onADFail(AdType.AM_NEW_INTERSTITIAL, i);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }

                @Override
                public void onAdLeftApplication() {
                    super.onAdLeftApplication();
                    onADClick(AdType.AM_NEW_INTERSTITIAL);
                }

                @Override
                public void onAdOpened() {
                    onADShow(AdType.AM_NEW_INTERSTITIAL);
                }

                @Override
                public void onAdLoaded() {
                    activeAd = interstitialAd;
                    onADFilledAndLoaded(AdType.AM_NEW_INTERSTITIAL);
                }
            });

            try {
                interstitialAd.load();
                onADLoad(AdType.AM_NEW_INTERSTITIAL);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }

            return true;
        }

        /**
         * 加载AdMob激励广告（新）
         *
         * @param ad
         * @return
         */
        private boolean loadAmNRawRewardAd(AMNRawRewardAd ad) {
            if (ad == null) {
                return false;
            }

            ad.setAdListener(new RewardAdListener() {
                @Override
                public void onRewardedAdLoaded(RawRewardAd ad) {
                    activeAd = ad;
                    onADFilledAndLoaded(AdType.AM_NEW_REWARD);
                }

                @Override
                public void onRewardedAdOpened(RawRewardAd ad) {
                    onADShow(AdType.AM_NEW_REWARD);
                }

                @Override
                public void onRewardedVideoStarted(RawRewardAd ad) {

                }

                @Override
                public void onRewardedAdClosed(RawRewardAd ad) {
                    onAdClose(AdType.AM_NEW_REWARD);
                }

                @Override
                public void onRewarded(RawRewardAd ad) {

                }

                @Override
                public void onRewardedAdLeftApplication(RawRewardAd ad) {
                    onADClick(AdType.AM_NEW_REWARD);
                }

                @Override
                public void onRewardedAdFailedToLoad(RawRewardAd ad, int code, String msg) {
                    onADFail(AdType.AM_NEW_REWARD, code);
                    currentAdIndex++;
                    if (!loadRawAd()) {
                        onRequestAllFail();
                    }
                }
            });

            try {
                ad.load();
                onADLoad(AdType.AM_NEW_REWARD);
            } catch (AdActionException e) {
                e.printStackTrace();
                return false;
            }

            return true;
        }


    }
}
